home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-22 | 6.6 KB | 276 lines | [TEXT/CWIE] |
- unit MyStrH;
-
- interface
-
- uses
- Types;
-
- type
- lineIndex = integer;
-
- const
- first_strh_offset = SizeOf(lineIndex);
-
- type
- indexPtr = ^lineIndex;
- StrHHandle = ^indexPtr;
-
- function NewStrH: StrHHandle;
- function GetStrH (id: integer): StrHHandle; { NoPurged, but not detached! }
- function Get1StrH (id: integer): StrHHandle; { NoPurged, but not detached! }
- procedure DisposeStrH (hhhh: StrHHandle);
- procedure ReleaseStrH (hhhh: StrHHandle);
- procedure ReinitStrH (hhhh: StrHHandle);
- function CountStrs (id: integer): lineIndex;
- function CountStrsH (hhhh: StrHHandle): lineIndex;
- function GetIndStr (id: integer; index: lineIndex): Str255;
- function GetIndStrH (hhhh: StrHHandle; index: lineIndex): Str255;
- function GetNextStrH (hhhh: StrHHandle; var offset: longint): Str255;
- procedure SetIndStr (id, index: lineIndex; s: Str255);
- procedure SetIndStrH (hhhh: StrHHandle; index: lineIndex; s: Str255);
- procedure AppendStrH (hhhh: StrHHandle; s: Str255);
- procedure DelIndStr (id: integer; index: lineIndex);
- procedure DelIndStrH (hhhh: StrHHandle; index: lineIndex);
- procedure InsIndString (id: integer; index: lineIndex; s: Str255);
- procedure InsIndStrH (hhhh: StrHHandle; index: integer; s: Str255);
- function ValidStringH (hhhh: StrHHandle): boolean;
- procedure ResetStrH (hhhh: StrHHandle);
-
- implementation
-
- uses
- Memory, Resources, ToolUtils, TextUtils;
-
- function NewStrH: StrHHandle;
- begin
- NewStrH := StrHHandle(NewHandleClear(SizeOf(lineIndex)));
- end;
-
- function GetStrH (id: integer): StrHHandle;
- var
- hhhh: Handle;
- begin
- hhhh := GetResource('STR#', id);
- if hhhh <> nil then begin
- HNoPurge(hhhh);
- end;
- GetStrH := StrHHandle(hhhh);
- end;
-
- function Get1StrH (id: integer): StrHHandle;
- var
- hhhh: Handle;
- begin
- hhhh := Get1Resource('STR#', id);
- if hhhh <> nil then begin
- HNoPurge(hhhh);
- end;
- Get1StrH := StrHHandle(hhhh);
- end;
-
- procedure DisposeStrH (hhhh: StrHHandle);
- begin
- DisposeHandle(Handle(hhhh));
- end;
-
- procedure ReleaseStrH (hhhh: StrHHandle);
- begin
- ReleaseResource(Handle(hhhh));
- end;
-
- procedure ReinitStrH (hhhh: StrHHandle);
- begin
- SetHandleSize(Handle(hhhh), SizeOf(lineIndex));
- hhhh^^ := 0;
- end;
-
- function CountStrsH (hhhh: StrHHandle): integer;
- begin
- CountStrsH := hhhh^^;
- end;
-
- function CountStrs (id: integer): lineIndex;
- var
- hhhh: StrHHandle;
- begin
- hhhh := StrHHandle(GetResource('STR#', id));
- CountStrs := hhhh^^;
- end;
-
- function GetIndStr (id: integer; index: lineIndex): Str255;
- var
- s: Str255;
- begin
- GetIndString(s, id, index);
- GetIndStr := s;
- end;
-
- function ValidStringH (hhhh: StrHHandle): boolean;
- var
- count, i: lineIndex;
- ps: longint;
- begin
- ValidStringH := false;
- if GetHandleSize(Handle(hhhh)) >= SizeOf(lineIndex) then begin
- count := hhhh^^;
- ps := SizeOf(lineIndex);
- for i := 1 to count do begin
- ps := ps + BAND(ptr(ord(hhhh^) + ps)^, $FF) + 1;
- end;
- ValidStringH := GetHandleSize(Handle(hhhh)) = ps;
- end;
- end;
-
- function GetIndStrH (hhhh: StrHHandle; index: lineIndex): Str255;
- var
- count, i: lineIndex;
- s: Str255;
- ps: longint;
- begin
- count := hhhh^^;
- if (1 <= index) and (index <= count) then begin
- ps := SizeOf(lineIndex);
- for i := 1 to index - 1 do begin
- ps := ps + BAND(ptr(ord(hhhh^) + ps)^, $FF) + 1;
- end;
- BlockMoveData(ptr(ord(hhhh^) + ps), @s, BAND(ptr(ord(hhhh^) + ps)^, $FF) + 1);
- end
- else begin
- s := '';
- end;
- GetIndStrH := s;
- end;
-
- function GetNextStrH (hhhh: StrHHandle; var offset: longint): Str255;
- var
- s: Str255;
- len: integer;
- begin
- if offset >= GetHandleSize(Handle(hhhh)) then begin
- s := '';
- end
- else begin
- len := BAND(ptr(ord(hhhh^) + offset)^, $FF);
- BlockMoveData(ptr(ord(hhhh^) + offset), @s, len + 1);
- offset := offset + len+1;
- end;
- GetNextStrH := s;
- end;
-
- procedure ResetStrH (hhhh: StrHHandle);
- begin
- SetHandleSize(Handle(hhhh), SizeOf(lineIndex));
- hhhh^^ := 0;
- end;
-
- procedure SetIndStrH (hhhh: StrHHandle; index: lineIndex; s: Str255);
- var
- count, i: lineIndex;
- sz: longint;
- p: longint;
- pos: longint;
- ps: longint;
- begin
- count := hhhh^^;
- sz := GetHandleSize(Handle(hhhh));
- if count < index then begin
- SetHandleSize(Handle(hhhh), sz + index - count);
- if MemError <> noErr then begin
- Exit(SetIndStrH);
- end;
- for p := ord(hhhh^) + sz to ord(hhhh^) + sz + index - count - 1 do begin
- ptr(p)^ := 0;
- end;
- hhhh^^ := index;
- count := index;
- end;
- ps := SizeOf(lineIndex);
- for i := 1 to index - 1 do begin
- ps := ps + BAND(ptr(ord(hhhh^) + ps)^, $FF) + 1;
- end;
- pos := Munger(Handle(hhhh), ps, nil, BAND(ptr(ord(hhhh^) + ps)^, $FF) + 1, @s, length(s) + 1);
- end;
-
- procedure AppendStrH (hhhh: StrHHandle; s: Str255);
- begin
- if PtrAndHand(@s, Handle(hhhh), length(s) + 1) = noErr then begin
- hhhh^^ := hhhh^^ + 1;
- end;
- end;
-
- procedure SetIndStr (id, index: lineIndex; s: Str255);
- var
- hhhh: StrHHandle;
- begin
- hhhh := StrHHandle(GetResource('STR#', id));
- HNoPurge(Handle(hhhh));
- SetIndStrH(hhhh, index, s);
- HPurge(Handle(hhhh));
- ChangedResource(Handle(hhhh));
- WriteResource(Handle(hhhh));
- end;
-
- procedure DelIndStrH (hhhh: StrHHandle; index: integer);
- var
- count, i: lineIndex;
- sz: longint;
- pos: longint;
- ps: longint;
- begin
- count := hhhh^^;
- sz := GetHandleSize(Handle(hhhh));
- if count >= index then begin
- ps := SizeOf(lineIndex);
- for i := 1 to index - 1 do begin
- ps := ps + BAND(ptr(ord(hhhh^) + ps)^, $FF) + 1;
- end;
- pos := Munger(Handle(hhhh), ps, nil, BAND(ptr(ord(hhhh^) + ps)^, $FF) + 1, @pos, 0); { @err is a safe, non nil addr }
- hhhh^^ := count - 1;
- end;
- end;
-
- procedure DelIndStr (id: integer; index: lineIndex);
- var
- hhhh: StrHHandle;
- begin
- hhhh := StrHHandle(GetResource('STR#', id));
- HNoPurge(Handle(hhhh));
- DelIndStrH(hhhh, index);
- HPurge(Handle(hhhh));
- ChangedResource(Handle(hhhh));
- WriteResource(Handle(hhhh));
- end;
-
- procedure InsIndStrH (hhhh: StrHHandle; index: integer; s: Str255);
- var
- count, i: lineIndex;
- pos: longint;
- ps: longint;
- t: string[2];
- begin
- count := hhhh^^;
- if count >= index then begin
- ps := SizeOf(lineIndex);
- for i := 1 to index - 1 do begin
- ps := ps + BAND(ptr(ord(hhhh^) + ps)^, $FF) + 1;
- end;
- t := '';
- pos := Munger(Handle(hhhh), ps, nil, 0, @t, length(t) + 1);
- hhhh^^ := count + 1;
- end;
- SetIndStrH(hhhh, index, s)
- end;
-
- procedure InsIndString (id: integer; index: lineIndex; s: Str255);
- var
- hhhh: StrHHandle;
- begin
- hhhh := StrHHandle(GetResource('STR#', id));
- HNoPurge(Handle(hhhh));
- InsIndStrH(hhhh, index, s);
- HPurge(Handle(hhhh));
- ChangedResource(Handle(hhhh));
- WriteResource(Handle(hhhh));
- end;
-
- end.